home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 12956 / 12956.xpi / chrome / fabtab.jar / content / fabtab / fabtab.js next >
Text File  |  2009-11-27  |  30KB  |  637 lines

  1. window.addEventListener("load", function(event) {FabTabOverlay.Initialize();}, false);
  2. window.addEventListener("unload", function(event) {FabTabOverlay.DeInitialize();}, false);
  3.  
  4. var FabTabWebProgressListener =
  5. {
  6.     QueryInterface: function(aIID)
  7.     {
  8.         if (aIID.equals(Components.interfaces.nsIWebProgressListener) || aIID.equals(Components.interfaces.nsISupportsWeakReference) || aIID.equals(Components.interfaces.nsISupports)) return this;
  9.         throw Components.results.NS_NOINTERFACE;
  10.     },
  11.  
  12.     onStateChange: function(aWebProgress, aRequest, aFlag, aStatus)
  13.     {
  14.         if(aFlag & Components.interfaces.nsIWebProgressListener.STATE_STOP && aFlag & Components.interfaces.nsIWebProgressListener.STATE_IS_NETWORK)
  15.         {
  16.             if (document.defaultView.FabTabOverlay)
  17.             {
  18.                 var aTabs = getBrowser().mTabs;
  19.                 var oTab;
  20.                 var iIndex, iLen = aTabs.length;
  21.                 for (iIndex = 0; iIndex < iLen; iIndex ++)
  22.                 {
  23.                     if (aTabs[iIndex].linkedBrowser && aTabs[iIndex].linkedBrowser.contentDocument.location ==  aWebProgress.DOMWindow.document.location)
  24.                     {
  25.                         oTab = aTabs[iIndex];
  26.                         setTimeout(function() {document.defaultView.FabTabOverlay.UpdateTabs(oTab);}, 100);
  27.                     }
  28.                 }
  29.             }
  30.         }
  31.         return 0;
  32.     },
  33.  
  34.     onLocationChange: function(aProgress, aRequest, aURI) {return 0;},
  35.     onProgressChange: function() {return 0;},
  36.     onStatusChange: function() {return 0;},
  37.     onSecurityChange: function() {return 0;},
  38.     onLinkIconAvailable: function() {return 0;}
  39. }
  40.  
  41. var FabTabOverlay =
  42. {
  43.     bInitialized:false,
  44.  
  45.     Initialize: function()
  46.     {
  47.         this.oPreferences = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  48.         this.oPreferences = this.oPreferences.getBranch("extensions.fabtab.");
  49.  
  50.         var oAppInfo = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULRuntime);
  51.         var oContainer = getBrowser().tabContainer;
  52.         if (oContainer)
  53.         {
  54.             this.bIsRunningUnderLinux = (oAppInfo.OS == "Linux");
  55.             var oTabMixPlusItem;
  56.             try
  57.             {
  58.                 var oEM = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
  59.                 if (oEM.getItemForID) {oTabMixPlusItem = oEM.getItemForID("{dc572301-7619-498c-a57d-39143191b318}");}
  60.                 else {oTabMixPlusItem = oEM.getItemList("{dc572301-7619-498c-a57d-39143191b318}", null, {})[0];}
  61.             } catch (e) {oTabMixPlusItem = null;}
  62.  
  63.             if (oTabMixPlusItem && this.bIsRunningUnderLinux)
  64.             {
  65.                 /*Tab Mix Plus under Linux fix...*/
  66.                 oContainer.setAttribute("fabtab_for_linux_tmp", "true");
  67.             }
  68.             else
  69.             {
  70.                 oContainer.setAttribute("fabtab_for_linux", this.bIsRunningUnderLinux);
  71.             }
  72.             oContainer.setAttribute("fabtab_colorbottombox", this.oPreferences.getBoolPref("colorbottombox"));
  73.         }
  74.  
  75.         if (this.bInitialized) return true;
  76.  
  77.         this.oPrefObserver =
  78.         {
  79.             observe: function(subject, topic, prefName)
  80.             {
  81.                 if (topic == "nsPref:changed" && prefName.indexOf("extensions.fabtab.") >= 0)
  82.                 {
  83.                     setTimeout(function() {FabTabOverlay.RefreshAll(false);}, 100);
  84.                 }
  85.             }
  86.         };
  87.  
  88.         this.bInitialized = true;
  89.  
  90.         this.oStrings = document.getElementById("fabtab-string-bundle");
  91.  
  92.         this.aPixelCache = null;
  93.         this.aPixelCache = [];
  94.  
  95.         var oPrefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranchInternal);
  96.         oPrefService.addObserver("extensions.fabtab.fuzzycolormatch", this.oPrefObserver, false);
  97.         oPrefService.addObserver("extensions.fabtab.x", this.oPrefObserver, false);
  98.         oPrefService.addObserver("extensions.fabtab.y", this.oPrefObserver, false);
  99.         oPrefService.addObserver("extensions.fabtab.width", this.oPrefObserver, false);
  100.         oPrefService.addObserver("extensions.fabtab.height", this.oPrefObserver, false);
  101.         oPrefService.addObserver("extensions.fabtab.preventwhite", this.oPrefObserver, false);
  102.         oPrefService.addObserver("extensions.fabtab.preventblack", this.oPrefObserver, false);
  103.         oPrefService.addObserver("extensions.fabtab.activeonly", this.oPrefObserver, false);
  104.         oPrefService.addObserver("extensions.fabtab.opacity", this.oPrefObserver, false);
  105.         oPrefService.addObserver("extensions.fabtab.activetabopacity", this.oPrefObserver, false);
  106.         oPrefService.addObserver("extensions.fabtab.disabletabbgimage", this.oPrefObserver, false);
  107.         oPrefService.addObserver("extensions.fabtab.whitetextthreshold", this.oPrefObserver, false);
  108.         oPrefService.addObserver("extensions.fabtab.hostnamecache", this.oPrefObserver, false);
  109.  
  110.         var oContainer = getBrowser().tabContainer;
  111.         oContainer.addEventListener("TabOpen", function(event) {event.originalTarget.linkedBrowser.addProgressListener(FabTabWebProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);}, false);
  112.         oContainer.addEventListener("TabClose", function(event) {event.originalTarget.linkedBrowser.removeProgressListener(FabTabWebProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);}, false);
  113.         oContainer.addEventListener("TabSelect", function(event) {FabTabOverlay.UpdateTabs();}, false);
  114.  
  115.         var aTabs = getBrowser().mTabs;
  116.         var iIndex, iLen = aTabs.length;
  117.         for (iIndex = 0; iIndex < iLen; iIndex ++)
  118.         {
  119.             aTabs[iIndex].linkedBrowser.addProgressListener(FabTabWebProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
  120.         }
  121.  
  122.         this.UpdateTabs();
  123.         this.InitializeTabContextMenu();
  124.     },
  125.  
  126.     InitializeTabContextMenu: function()
  127.     {
  128.         if (!this.oPreferences.getBoolPref("hidecmitems"))
  129.         {
  130.             if (!document.getElementById('fabtab-context-clear'))
  131.             {
  132.                 var oMenuPopup = document.getAnonymousElementByAttribute(gBrowser, "anonid", "tabContextMenu")
  133.                 var oElement;
  134.  
  135.                 oElement = document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', "menuseparator");
  136.                 oMenuPopup.appendChild(oElement);
  137.  
  138.                 oElement = document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', "menuitem");
  139.                 oElement.setAttribute("id", "fabtab-context-clear");
  140.                 oElement.setAttribute("label", this.GetString("fabtab.context.clear"));
  141.                 oElement.setAttribute("oncommand", "FabTabOverlay.RefreshAll(true);");
  142.  
  143.                 oMenuPopup.appendChild(oElement);
  144.  
  145.                 oElement = document.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', "menuitem");
  146.                 oElement.setAttribute("id", "fabtab-context-clearall");
  147.                 oElement.setAttribute("label", this.GetString("fabtab.context.clearall"));
  148.                 oElement.setAttribute("oncommand", "FabTabOverlay.RefreshAll(false);");
  149.                 oMenuPopup.appendChild(oElement);
  150.             }
  151.         }
  152.     },
  153.  
  154.     DeInitialize: function()
  155.     {
  156.         var oContainer = getBrowser().tabContainer;
  157.         oContainer.removeEventListener("TabOpen", function(event) {event.originalTarget.linkedBrowser.addProgressListener(FabTabWebProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);}, false);
  158.         oContainer.removeEventListener("TabClose", function(event) {event.originalTarget.linkedBrowser.removeProgressListener(FabTabWebProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);}, false);
  159.  
  160.         var oPrefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranchInternal);
  161.         oPrefService.removeObserver("extensions.fabtab.fuzzycolormatch", this.oPrefObserver, false);
  162.         oPrefService.removeObserver("extensions.fabtab.x", this.oPrefObserver, false);
  163.         oPrefService.removeObserver("extensions.fabtab.y", this.oPrefObserver, false);
  164.         oPrefService.removeObserver("extensions.fabtab.width", this.oPrefObserver, false);
  165.         oPrefService.removeObserver("extensions.fabtab.height", this.oPrefObserver, false);
  166.         oPrefService.removeObserver("extensions.fabtab.preventwhite", this.oPrefObserver, false);
  167.         oPrefService.removeObserver("extensions.fabtab.preventblack", this.oPrefObserver, false);
  168.         oPrefService.removeObserver("extensions.fabtab.activeonly", this.oPrefObserver, false);
  169.         oPrefService.removeObserver("extensions.fabtab.opacity", this.oPrefObserver, false);
  170.         oPrefService.removeObserver("extensions.fabtab.activetabopacity", this.oPrefObserver, false);
  171.         oPrefService.removeObserver("extensions.fabtab.disabletabbgimage", this.oPrefObserver, false);
  172.         oPrefService.removeObserver("extensions.fabtab.whitetextthreshold", this.oPrefObserver, false);
  173.         oPrefService.removeObserver("extensions.fabtab.hostnamecache", this.oPrefObserver, false);
  174.  
  175.         this.bInitialized = false;
  176.     },
  177.  
  178.     RefreshAll: function(bResetCurrentOnly)
  179.     {
  180.         if (bResetCurrentOnly)
  181.         {
  182.             this.aPixelCache[this.GetURL(getBrowser().selectedTab.linkedBrowser.contentDocument.URL)] = "";
  183.         }
  184.         else
  185.         {
  186.             this.aPixelCache = null;
  187.             this.aPixelCache = [];
  188.         }
  189.  
  190.         this.UpdateTabs();
  191.     },
  192.  
  193.     UpdateTabs: function(oTab)
  194.     {
  195.         var bActiveOnly = this.oPreferences.getBoolPref("activeonly");
  196.         var oTabs;
  197.         if (oTab && !bActiveOnly) oTabs = new Array(oTab);
  198.         else oTabs = getBrowser().mTabs;
  199.  
  200.         var oWindow;
  201.         var oCanvas = document.getElementById("fabtab-canvas");
  202.         var oContext = oCanvas.getContext("2d");
  203.         var oImageData;
  204.         var oPixelArray;
  205.         var oTabPreview;
  206.         var aPixels;
  207.         var aRGBValues = new Array();
  208.         var aRGBValue;
  209.         var sDataURL, sPixel, sContrastTextColor;
  210.         var iPIndex, iPLen, iPixel, iWidth, iHeight;
  211.         var iIndex, iFMCount, iLen = oTabs.length;
  212.         var iFuzzyColorMatch = this.oPreferences.getIntPref("fuzzycolormatch");
  213.         var iPreventWhite = this.oPreferences.getIntPref("preventwhite");
  214.         var iPreventBlack = this.oPreferences.getIntPref("preventblack");
  215.         var iOpacity = this.oPreferences.getIntPref("opacity") / 100;
  216.         var iATOpacity = this.oPreferences.getIntPref("activetabopacity") / 100;
  217.         var iNewOpacity = 100;
  218.         var iX = this.oPreferences.getIntPref("x");
  219.         var iY = this.oPreferences.getIntPref("y");
  220.         var iWidth = this.oPreferences.getIntPref("width");
  221.         var iHeight = this.oPreferences.getIntPref("height");
  222.         var iCWidth = (iWidth / 2);
  223.         var iCHeight = (iHeight / 2);
  224.         var bRemoveBGImage = this.oPreferences.getBoolPref("disabletabbgimage");
  225.  
  226.         var oContainer = getBrowser().tabContainer;
  227.         if (oContainer)
  228.         {
  229.             oContainer.setAttribute("fabtab_nobgimage", bRemoveBGImage);
  230.         }
  231.  
  232.         for (iIndex = 0; iIndex < iLen; iIndex ++)
  233.         {
  234.             aPixels = null;
  235.             aPixels = [];
  236.  
  237.             aRGBValues = null;
  238.             aRGBValues = new Array();
  239.             if (oTabs[iIndex].localName != "tab") continue;
  240.             oWindow = oTabs[iIndex].linkedBrowser.contentWindow;
  241.             if (oWindow)
  242.             {
  243.                 try
  244.                 {
  245.                     sPixel = "";
  246.                     try {sPixel = this.aPixelCache[this.GetURL(oTabs[iIndex].linkedBrowser.contentDocument.URL)];}
  247.                     catch (e) {}
  248.  
  249.                     if (this.GetURL(oTabs[iIndex].linkedBrowser.contentDocument.URL) == "about:blank") sPixel = "rgb(255,255,255)";
  250.  
  251.                     if (!sPixel || sPixel == "")
  252.                     {
  253.                         if (iWidth > (oWindow.innerWidth - 25)) iWidth = oWindow.innerWidth - 25;
  254.                         if (iHeight > (oWindow.innerHeight - 25)) iHeight = oWindow.innerHeight - 25;
  255.                         iCWidth = parseInt(iWidth / 2);
  256.                         iCHeight = parseInt(iHeight / 2);
  257.  
  258.                         oCanvas.style.width = iCWidth + "px";
  259.                         oCanvas.style.height = iCHeight + "px";
  260.                         oCanvas.width = iCWidth;
  261.                         oCanvas.height = iCHeight;
  262.  
  263.                         oContext.clearRect(0, 0, iCWidth, iCHeight);
  264.                         oContext.save();
  265.                         oContext.scale(iCWidth/iWidth, iCHeight/iHeight);
  266.                         this.HideFrames(oWindow, true);
  267.                         oContext.drawWindow(oWindow, iX, iY, iWidth, iHeight, "rgb(0,0,0)");
  268.                         this.HideFrames(oWindow, false);
  269.                         oContext.restore();
  270.                         oImageData = oContext.getImageData(0,0,iCWidth,iCHeight);
  271.  
  272. /*
  273.                         var oFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
  274.                         if (!this.iRefreshIndex) this.iRefreshIndex = 0;
  275.                         oFile.initWithPath("c:\\" + getBrowser().getBrowserIndexForDocument(oTabs[iIndex].linkedBrowser.contentDocument) + "_" + this.iRefreshIndex + ".png");
  276.                         var io = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  277.                         var source = io.newURI(oCanvas.toDataURL("image/png", ""), "UTF8", null);
  278.                         var target = io.newFileURI(oFile);
  279.                         var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Components.interfaces.nsIWebBrowserPersist);
  280.                         persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
  281.                         persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
  282.                         persist.saveURI(source, null, null, null, null, oFile);
  283. */
  284.  
  285.                         oPixelArray = oImageData.data;
  286.                         iPLen = oPixelArray.length;
  287.                         for (iPIndex = 0; iPIndex < iPLen - 3; iPIndex += 4)
  288.                         {
  289.                             sPixel = "rgb(" + oPixelArray[iPIndex] + "," + oPixelArray[iPIndex + 1] + "," + oPixelArray[iPIndex + 2] + ")";
  290.                             if (this.AllowColor(oPixelArray[iPIndex], oPixelArray[iPIndex + 1], oPixelArray[iPIndex + 2], iPreventWhite, iPreventBlack))
  291.                             {
  292.                                 try
  293.                                 {
  294.                                     iPixel = aPixels[sPixel];
  295.                                     if (!iPixel) iPixel = 0;
  296.                                     if (isNaN(iPixel)) iPixel = 0;
  297.                                     aPixels[sPixel] = iPixel + 1;
  298.                                 }
  299.                                 catch (e)
  300.                                 {
  301.                                     aPixels[sPixel] = 1;
  302.                                 }
  303.                             }
  304.                         }
  305.  
  306.                         aPixels = this.SortRGBArray(aPixels);
  307.                         if (this.GetRGBValue(aPixels, 0))
  308.                         {
  309.                             for (iPIndex = 0; iPIndex < 32; iPIndex ++)
  310.                             {
  311.                                 try
  312.                                 {
  313.                                     sPixel = this.GetRGBValue(aPixels, iPIndex);
  314.                                     if (sPixel)
  315.                                     {
  316.                                         aRGBValue = this.RGBValueToRGBArray(sPixel);
  317.                                         aRGBValues[aRGBValues.length] = new Array(aRGBValue, aPixels[sPixel]);
  318.                                     }
  319.                                 }
  320.                                 catch (e) {break;}
  321.                             }
  322.                             aPixels = null;
  323.                             aPixels = [];
  324.  
  325.                             iPLen = aRGBValues.length;
  326.                             for (iPIndex = 0; iPIndex < iPLen; iPIndex ++)
  327.                             {
  328.                                 iFMCount = this.GetFuzzyMatchColorCount(aRGBValues, aRGBValues[iPIndex], iFuzzyColorMatch);
  329.                                 aPixels[this.RGBArrayToRGBValue(aRGBValues[iPIndex][0])] = (iFMCount * aRGBValues[iPIndex][1]);
  330.                                 aRGBValues[iPIndex][2] = iFMCount;
  331.                             }
  332.  
  333.                             aPixels = this.SortRGBArray(aPixels);
  334.  
  335.                             sPixel = this.GetRGBValue(aPixels, 0);
  336.                             this.aPixelCache[this.GetURL(oTabs[iIndex].linkedBrowser.contentDocument.URL)] = sPixel;
  337.                         }
  338.                     }
  339.  
  340.                     if (!bActiveOnly || oTabs[iIndex].linkedPanel == getBrowser().selectedTab.linkedPanel)
  341.                     {
  342.                         iNewOpacity = iOpacity;
  343.                         if (oTabs[iIndex].linkedPanel == getBrowser().selectedTab.linkedPanel)
  344.                         {
  345.                             iNewOpacity = iATOpacity;
  346.                             var oBottomBox = document.getElementById("fabtabs-bottom");
  347.                             if (oBottomBox)
  348.                             {
  349.                                 oBottomBox.setAttribute("style", "background-color: " + sPixel + " !important;");
  350.                             }
  351.                         }
  352.  
  353.                         sContrastTextColor = this.GetContrastTextColor(sPixel)
  354.  
  355.                         /*Color the tab*/
  356.                         oTabs[iIndex].style.setProperty("background-color", sPixel, "important");
  357.                         oTabs[iIndex].style.setProperty("color", sContrastTextColor, "important");
  358.                         oTabs[iIndex].style.setProperty("opacity", iNewOpacity, "important");
  359.  
  360.                         /*Force color the text node*/
  361.                         var oTabText = document.getAnonymousElementByAttribute(oTabs[iIndex], "class", "tab-text");
  362.                         if (oTabText) oTabText.style.setProperty("color", sContrastTextColor, "important");
  363.  
  364.                         /*Color any anonymous child nodes.*/
  365.                         if (bRemoveBGImage) this.ColorAnonymousChildNodes(oTabs[iIndex], sPixel);
  366.  
  367.                         /*Color the Linux XBL bound tab box*/
  368.                         if (oTabs[iIndex].mFabTabBox)
  369.                         {
  370.                             oTabs[iIndex].mFabTabBox.setAttribute("style", "background-color: " + sPixel + " !important;");
  371.                         }
  372.  
  373.                         /*See if we can color Tab Sidebar previews as well...*/
  374.                         try
  375.                         {
  376.                             oTabPreview = document.getElementsByAttribute("browser", oTabs[iIndex].getAttribute("linkedpanel"))[0];
  377.                             if (!oTabPreview) oTabPreview = document.getElementById("sidebar").contentDocument.getElementsByAttribute("browser", oTabs[iIndex].getAttribute("linkedpanel"))[0];
  378.                             if (oTabPreview)
  379.                             {
  380.                                 oTabPreview.style.setProperty("background-color", sPixel, "important");
  381.                                 oTabPreview.style.setProperty("color", sContrastTextColor, "important");
  382.                                 oTabPreview.style.setProperty("opacity", iNewOpacity, "important");
  383.                             }
  384.                         } catch (e) {/*Sidebar closed and/or Tab Preview not installed...*/}
  385.                     }
  386.                     else
  387.                     {
  388.                         /*Remove the tab styles again*/
  389.                         oTabs[iIndex].style.backgroundColor = "";
  390.                         oTabs[iIndex].style.color = "";
  391.                         oTabs[iIndex].style.opacity = "";
  392.  
  393.                         /*Force remove the color on the text node*/
  394.                         var oTabText = document.getAnonymousElementByAttribute(oTabs[iIndex], "class", "tab-text");
  395.                         if (oTabText) oTabText.style.color = "";
  396.  
  397.                         /*And remove them from any anonymous childs*/
  398.                         if (bRemoveBGImage) this.ColorAnonymousChildNodes(oTabs[iIndex], null, null);
  399.  
  400.                         /*Remove it from the XBL Linux only box*/
  401.                         if (oTabs[iIndex].mFabTabBox)
  402.                         {
  403.                             oTabs[iIndex].mFabTabBox.removeAttribute("style");
  404.                         }
  405.  
  406.                         /*See if we can color Tab Sidebar previews as well...*/
  407.                         try
  408.                         {
  409.                             oTabPreview = document.getElementsByAttribute("browser", oTabs[iIndex].getAttribute("linkedpanel"))[0];
  410.                             if (!oTabPreview) oTabPreview = document.getElementById("sidebar").contentDocument.getElementsByAttribute("browser", oTabs[iIndex].getAttribute("linkedpanel"))[0];
  411.                             if (oTabPreview)
  412.                             {
  413.                                 oTabPreview.style.backgroundColor = "";
  414.                                 oTabPreview.style.color = "";
  415.                                 oTabPreview.style.opacity = "";
  416.                             }
  417.                         } catch (e) {/*Sidebar closed and/or Tab Preview not installed...*/}
  418.                     }
  419.                 }
  420.                 catch (e) {this.WriteDebugMessage(e);}
  421.             }
  422.         }
  423.     },
  424.  
  425.     ColorAnonymousChildNodes: function(oParentNode, sBackgroundColor, sTextColor)
  426.     {
  427.         var oNodes = document.getAnonymousNodes(oParentNode);
  428.  
  429.         if (oNodes)
  430.         {
  431.             var iIndex, iLen = oNodes.length;
  432.  
  433.             for (iIndex = 0; iIndex < iLen; iIndex ++)
  434.             {
  435.                 if (oNodes[iIndex])
  436.                 {
  437.                     if (sBackgroundColor)
  438.                     {
  439.                         oNodes[iIndex].style.setProperty("background-color", sBackgroundColor, "important");
  440.                         oNodes[iIndex].style.setProperty("color", sTextColor, "important");
  441.                     }
  442.                     else
  443.                     {
  444.                         oNodes[iIndex].style.backgroundColor = "";
  445.                         oNodes[iIndex].style.color = "";
  446.                     }
  447.  
  448.                     this.ColorAnonymousChildNodes(oNodes[iIndex], sBackgroundColor, sTextColor);
  449.                 }
  450.             }
  451.         }
  452.     },
  453.  
  454.     HideFrames: function(oWindow, bHide)
  455.     {
  456.         var aFrames = oWindow.document.getElementsByTagName("IFRAME");
  457.         var iIndex, iLen = aFrames.length;
  458.  
  459.         for (iIndex = 0; iIndex < iLen; iIndex ++)
  460.         {
  461.             try
  462.             {
  463.                 if (bHide)
  464.                 {
  465.                     if (aFrames[iIndex].contentDocument.location.host != oWindow.document.location.host)
  466.                     {
  467.                         aFrames[iIndex].setAttribute("fabtabdisplay", aFrames[iIndex].style.display);
  468.                         aFrames[iIndex].style.display = "none";
  469.                     }
  470.                 }
  471.                 else
  472.                 {
  473.                     if (aFrames[iIndex].contentDocument.location.host != oWindow.document.location.host)
  474.                     {
  475.                         aFrames[iIndex].style.display = aFrames[iIndex].getAttribute("fabtabdisplay");
  476.                         aFrames[iIndex].removeAttribute("fabtabdisplay");
  477.                     }
  478.                 }
  479.             } catch (e) {}
  480.         }
  481.     },
  482.  
  483.     HandleTabSelect: function(oTab)
  484.     {
  485.         if (oTab) this.UpdateTabs();
  486.     },
  487.  
  488.     FindInPixelArray: function(aArray, sFind)
  489.     {
  490.         var iIndex, iLen = aArray.length;
  491.         for (iIndex = 0; iIndex < iLen; iIndex ++) if (aArray[iIndex][0] == sFind) return iIndex;
  492.         return -1;
  493.     },
  494.  
  495.     GetFuzzyMatchColorCount: function(aRGBValues, aRGBValue, iFuzzyColorMatch)
  496.     {
  497.         var iROrg = aRGBValue[0][0];
  498.         var iGOrg = aRGBValue[0][1];
  499.         var iBOrg = aRGBValue[0][2];
  500.         var iIndex, iLen;
  501.         var iRDiff;
  502.         var iGDiff;
  503.         var iBDiff;
  504.         var iDiff;
  505.         var iCount = 0;
  506.  
  507.         iLen = aRGBValues.length;
  508.  
  509.         for (iIndex = 0; iIndex < iLen; iIndex ++)
  510.         {
  511.             iRDiff = Math.pow((iROrg - aRGBValues[iIndex][0][0]), 2);
  512.             iGDiff = Math.pow((iGOrg - aRGBValues[iIndex][0][1]), 2);
  513.             iBDiff = Math.pow((iBOrg - aRGBValues[iIndex][0][2]), 2);
  514.  
  515.  
  516.             iDiff = Math.sqrt(iRDiff + iGDiff + iBDiff);
  517.             if (iDiff <= iFuzzyColorMatch)
  518.             {
  519.                 iCount ++;
  520.             }
  521.         }
  522.         return iCount;
  523.     },
  524.  
  525.     RGBValueToRGBArray: function(sRGB)
  526.     {
  527.         sRGB = sRGB.replace("rgb(", "").replace(")", "");
  528.         var aRGB = sRGB.split(",");
  529.         var iIndex, iLen = aRGB.length;
  530.         for (iIndex = 0; iIndex < iLen; iIndex ++) aRGB[iIndex] = parseInt(aRGB[iIndex]);
  531.  
  532.         return aRGB;
  533.     },
  534.  
  535.     RGBArrayToRGBValue: function(aRGB)
  536.     {
  537.         return "rgb(" + aRGB[0] + "," + aRGB[1] + "," + aRGB[2] + ")";
  538.     },
  539.  
  540.     SortRGBArray: function(aInput)
  541.     {
  542.         var aOutput = [];
  543.         var aTemp = [];
  544.         var sKey;
  545.         var iIndex;
  546.  
  547.         for (sKey in aInput) aTemp.push([sKey, aInput[sKey]]);
  548.         aTemp.sort(function () {return arguments[0][1] > arguments[1][1]});
  549.  
  550.         for (iIndex = aTemp.length - 1; iIndex >= 0; iIndex --) aOutput[aTemp[iIndex][0]] = aTemp[iIndex][1];
  551.  
  552.         return aOutput;
  553.     },
  554.  
  555.     GetRGBValue: function(aRGBArray, iPosition)
  556.     {
  557.         var sRGBValue;
  558.         var iIndex = 0;
  559.         for (sRGBValue in aRGBArray)
  560.         {
  561.             if (iIndex == iPosition) return sRGBValue;
  562.             iIndex ++;
  563.         }
  564.         return "";
  565.     },
  566.  
  567.     AllowColor: function(iR, iG, iB, iPreventWhite, iPreventBlack)
  568.     {
  569.         if (iR > (255 - iPreventWhite) && iG > (255 - iPreventWhite) && iB > (255 - iPreventWhite)) return false;
  570.         if (iR < iPreventBlack && iG < iPreventBlack && iB < iPreventBlack) return false;
  571.         return true;
  572.     },
  573.  
  574.     GetContrastTextColor: function(sRGB)
  575.     {
  576.         sRGB = sRGB.replace('rgb(', '');
  577.         sRGB = sRGB.replace(')', '');
  578.         var aRGB = sRGB.split(',');
  579.         var iR = parseInt(aRGB[0]);
  580.         var iG = parseInt(aRGB[1]);
  581.         var iB = parseInt(aRGB[2]);
  582.  
  583.         var iBackY = ((iR * 299) + (iG * 587) + (iB * 114)) / 1000;
  584.         var iTextY = ((0 * 299) + (0 * 587) + (0 * 114)) / 1000;
  585.  
  586.         var iBDiff = Math.abs(iBackY - iTextY);
  587.         var iCDiff = iR + iG + iB;
  588.  
  589.         var iThreshold = this.oPreferences.getIntPref("whitetextthreshold");
  590.         if (iBDiff < iThreshold && iCDiff <= (iThreshold * 2)) return "rgb(255,255,255)"
  591.         return "rgb(0,0,0)"
  592.     },
  593.  
  594.     GetURL: function(sURL)
  595.     {
  596.         if (!this.oPreferences.getBoolPref("hostnamecache")) return sURL;
  597.  
  598.         var sHost = sURL;
  599.         try
  600.         {
  601.             var oIOS = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  602.             var oURI = oIOS.newURI(sURL,null,null);
  603.             sHost = oURI.host;
  604.         }
  605.         catch (e) {}
  606.  
  607.         return sHost;
  608.     },
  609.  
  610.     GetString: function(sName, sVar1, sVar2, sVar3, sVar4, sVar5, sVar6, sVar7)
  611.     {
  612.         var sbService = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService);
  613.         var oStringBundle = sbService.createBundle("chrome://fabtab/locale/fabtab.properties");
  614.  
  615.         var sResult = "";
  616.  
  617.         if(oStringBundle)
  618.         {
  619.             sResult  = oStringBundle.GetStringFromName(sName);
  620.             if (sVar1 || (typeof(sVar1) == "number" && sVar1 == 0)) sResult = sResult.replace(/%1/g, sVar1);
  621.             if (sVar2 || (typeof(sVar2) == "number" && sVar2 == 0)) sResult = sResult.replace(/%2/g, sVar2);
  622.             if (sVar3 || (typeof(sVar3) == "number" && sVar3 == 0)) sResult = sResult.replace(/%3/g, sVar3);
  623.             if (sVar4 || (typeof(sVar4) == "number" && sVar4 == 0)) sResult = sResult.replace(/%4/g, sVar4);
  624.             if (sVar5 || (typeof(sVar5) == "number" && sVar5 == 0)) sResult = sResult.replace(/%5/g, sVar5);
  625.             if (sVar6 || (typeof(sVar6) == "number" && sVar6 == 0)) sResult = sResult.replace(/%6/g, sVar6);
  626.             if (sVar7 || (typeof(sVar7) == "number" && sVar7 == 0)) sResult = sResult.replace(/%7/g, sVar7);
  627.         }
  628.         return sResult;
  629.     },
  630.  
  631.     WriteDebugMessage: function(aMsg)
  632.     {
  633.         var oConsole = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces["nsIConsoleService"]);
  634.         oConsole.logStringMessage(aMsg);
  635.     }
  636. };
  637.